package com.ubao.network;




public class HttpMonitor extends Thread {
    //超时时间(毫秒)
    private long timeout;
    private Timeable timeable;
    private long startTime;
    private boolean active = false;
    private boolean timing = false;
    private boolean bPaused = false;

    /**
     * Creates a <code>SocketMonitor</code> for the provided <code>Timeable
     * </code> object with a timeout provided in seconds.
     * @param t a <code>Timeable</code> instance for this <code>RequestTimer</code>
     * to monitor.
     * @param timeout an <code>int</code> setting the timeout value in seconds
     */
    public HttpMonitor(Timeable t, int timeout) {
        this.timeout = timeout * 1000;
        timeable = t;
        timing = false;
    }

    public void pauseMonitor(){
        bPaused = true;
    }

    public void resumeMonitor(){
        startTime = System.currentTimeMillis();
        bPaused = false;
    }

    /**
     * The <code>Thread#run()</code> method for this <code>SocketMonitor</code>.
     * The <code>SocketMonitor#start()</code> method must be called to initiate
     * this object as a <code>Thread</code> prior to calling the startTimer()
     * method.
     */
    public void run() {
        active = true;
//        timing = false;
//        startTime = System.currentTimeMillis();

        while (active) {
            //if timeout is working
            if (timing) {
                long tmp = System.currentTimeMillis() - startTime;
                if (!bPaused && (tmp) > timeout) {
                    timeable.timeout();
                    stopTimer();
                } else {
                    try {
                        sleep(HttpConstants.MONITOR_INTERVAL);
                    } catch (InterruptedException ie) {
                        active = false;
                    }
                }
            } else {
                try {
                    this.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace() ;
                    active = false;
                }
            }
        }
    }

    /**
     * A synchronized method for the <code>SocketMonitor</code> and classes
     * that call this class to coordinate access to the value that is referenced
     * as the <code>SocketMonitor</code> start time.
     * @param t a <code>long</code> referenced to determine if the start time
     * attribute is to be accessed with a new value or the value last set.  Calling
     * this method with t=0 will return the current start time value.  Calling
     * this method with t=n where n > 0 will access the start time value set to n.
     * @return a <code>long</code> representing the value of the start time
     * attribute.
     */
//    public synchronized long getStartTime(long t) {
//        if (t != 0L) {
//            startTime = t;
//        }
//        return startTime;
//    }

//    private long startTime = 0;
    /**
     * This method activates the <code>SocketMonitor</code> to monitor for a
     * timeout condition.  The start time attribute is set to the current time.
     */
    public void startTimer() {
        if (!timing) {
            startTime = System.currentTimeMillis();
            timing = true;
//            synchronized (this) {
//                notifyAll();
//            }
        }
    }

    public void setTimeout(){
        try {
            stopTimer();
            timeable.timeout();
        } catch (Exception ex) {
        }
    }

    /**
     * Deactivates the <code>SocketMonitor</code> from monitoring for timeout
     * conditions.
     */
    public void stopTimer() {
        timing = false;
    }

    /**
     * This method should be called when the <code>SocketMonitor</code> is no longer
     * needed.  Calling this method will set an internal flag which will result
     * the run() method to exit.
     */
    public void cancelTimer() {
        active = false;
        timing = false;
        synchronized (this) {
            notifyAll();
        }
    }

    public long getTimeout(){
        return timeout/1000;
    }
}
